from pywinauto.keyboard import send_keys

from common.bus_related import add_all_bus, add_bus
from common.create_icd_signal import create_all_single
from constants.constant_config import MAIN_WINDOW, APP_PATH, APP_DIR
from utils.program import Program

# 连接程序
program = Program(APP_PATH, APP_DIR, MAIN_WINDOW)
app = program.connect_program()
main_win = app.window(title=MAIN_WINDOW)


# 创建项目节点
def create_project(model, project_name):
    # app.window(title=MAIN_WINDOW).child_window(title="项目", control_type="MenuItem").click_input()
    # app.window(control_type="Menu").child_window(title="新建项目", control_type="MenuItem").click_input()
    # 模板选择
    win1 = app.window(title="新建项目", control_type="Window").child_window(title=" Down",control_type="ComboBox")
    win1.click_input()
    win2 = win1.child_window(title=model, control_type="ListItem")
    win2.click_input()
    program.operation_window(win_name="新建项目", params={'input': [('名称：', project_name)], 'click': ["确定"]})


# 创建成品节点
def create_product(project_name, product_name):
    control = app.window().child_window(title=project_name, control_type="TreeItem", found_index=0)
    control.click_input(button='right', double=False)
    # 通过窗口标题获取顶层窗口
    top_window = app.top_window()
    # 点击右键弹出的菜单项
    menu_item = top_window.child_window(title="新建成品", control_type="MenuItem")
    # 左键点击菜单项
    menu_item.click_input()
    program.operation_window(win_name="创建新成品", params={'input': [('名称：', product_name)], 'click': ["确定"]})


# 创建插头组节点
def create_plugs(product_name):
    control = app.window().child_window(title=product_name, control_type="TreeItem", found_index=0)
    control.click_input(button='right', double=False)
    # 通过窗口标题获取顶层窗口
    top_window = app.top_window()
    # 点击右键弹出的菜单项
    menu_item = top_window.child_window(title="新建插头组", control_type="MenuItem")
    # 左键点击菜单项
    menu_item.click_input()
    program.operation_window(win_name="创建新插头组", params={'click': ["确定"]})


# 创建插头节点
def create_plug(plug_name):
    control = app.window().child_window(title="插头组", control_type="TreeItem", found_index=0)
    control.click_input(button='right', double=False)
    # 通过窗口标题获取顶层窗口
    top_window = app.top_window()
    # 点击右键弹出的菜单项
    menu_item = top_window.child_window(title="新建Plug", control_type="MenuItem")
    # 左键点击菜单项
    menu_item.click_input()
    program.operation_window(win_name="创建新Plug", params={'input': [('名称：', plug_name)], 'click': ["确定"]})


# 创建总线节点
def create_bus_point(plug_name, point_type, bus_name, tree_name):
    control = app.window().child_window(title=plug_name, control_type="TreeItem", found_index=0)
    control.click_input(button='right', double=False)
    # 通过窗口标题获取顶层窗口
    top_window = app.top_window()
    # 点击右键弹出的菜单
    if point_type == "1394":
        menu_item = top_window.child_window(title="新建1394节点", control_type="MenuItem")
        menu_item.click_input()
    elif point_type == "1553":
        menu_item = top_window.child_window(title="新建1553节点", control_type="MenuItem")
        menu_item.click_input()
    elif point_type == "422":
        menu_item = top_window.child_window(title="新建422节点", control_type="MenuItem")
        menu_item.click_input()
    elif point_type == "TTE":
        menu_item = top_window.child_window(title="新建TTE节点", control_type="MenuItem")
        menu_item.click_input()
    # 总线选择
    win = app.window(title="选择总线", control_type="Window", found_index=0)
    option = win.child_window(title=" Down", control_type="ComboBox")
    option.click_input()
    option.child_window(title=bus_name, control_type="ListItem").click_input()
    win.child_window(title="确定 Enter", control_type="Button").click_input()
    if point_type == "1394":
        program.operation_window(win_name="创建新1394节点", params={'input': [('名称：', tree_name)], 'click': ["确定"]})
    elif point_type == "1553":
        program.operation_window(win_name="创建新1553节点", params={'input': [('名称：', tree_name)], 'click': ["确定"]})
    elif point_type == "422":
        program.operation_window(win_name="创建新422节点", params={'input': [('名称：', tree_name)], 'click': ["确定"]})
    elif point_type == "TTE":
        program.operation_window(win_name="创建新TTE节点", params={'input': [('名称：', tree_name)], 'click': ["确定"]})


# 创建空的ICD数据包
def create_icd_empty(icd_input_index, icd_type, icd_name, msg_id):
    # 输入的索引从0开始
    control = app.window().child_window(title="输出", control_type="TreeItem", found_index=icd_input_index)
    control.click_input(button='right', double=False)
    # 通过窗口标题获取顶层窗口
    top_window = app.top_window()
    # 点击右键弹出的菜单
    if icd_type == "1394CC":
        menu_item = top_window.child_window(title="新建ICD", control_type="MenuItem")
        menu_item.click_input()
    elif icd_type == "1394CCDL":
        menu_item = top_window.child_window(title="新建CCDL包", control_type="MenuItem")
        menu_item.click_input()
    elif icd_type == "1553":
        menu_item = top_window.child_window(title="新建1553包", control_type="MenuItem")
        menu_item.click_input()
    elif icd_type == "422":
        menu_item = top_window.child_window(title="新建422包", control_type="MenuItem")
        menu_item.click_input()
    elif icd_type == "TTE":
        menu_item = top_window.child_window(title="新建TTE包", control_type="MenuItem")
        menu_item.click_input()
    elif icd_type == "分区表":
        menu_item = top_window.child_window(title="新建分区表", control_type="MenuItem")
        menu_item.click_input()
    app.window().child_window(title="属性信息", control_type="TabItem", found_index=0).click_input()
    program.operation_window(params={'input': [('数据包名称:', icd_name), ('消息ID:', msg_id)], 'click': ["域信息"]})
    app.window().child_window(title="保存数据", control_type="Button", found_index=0).click_input()


def create_icd_and_single(icd_input_index, icd_type, icd_name, msg_id, byte_way):
    """
    创建包含信号的数据包
            icd_input_index：输入索引位置
            icd_type：ICD类型
            icd_name：ICD命名
            msg_id：消息ID
            byte_way：字节对齐方式（跟系统一样方便创建信号）
    """
    create_icd_empty(icd_input_index, icd_type, icd_name, msg_id)
    main_win.child_window(title="显示空白", control_type="Button").click_input()
    create_all_single(icd_type, byte_way)
    app.window().child_window(title="保存数据", control_type="Button", found_index=0).click_input()


# 创建分区组节点
def create_zones(product_name):
    control = app.window().child_window(title=product_name, control_type="TreeItem", found_index=0)
    control.click_input(button='right', double=False)
    # 通过窗口标题获取顶层窗口
    top_window = app.top_window()
    # 点击右键弹出的菜单项
    menu_item = top_window.child_window(title="新建分区组", control_type="MenuItem")
    # 左键点击菜单项
    menu_item.click_input()
    program.operation_window(win_name="创建新分区组", params={'click': ["确定"]})


def create_zone(project_model, zone_name, cpu_name=None):
    """
    创建分区节点
        model：项目模板
        zone_name：分区命名
        cpu_name：当模板为V100时有CPU，其他模板没用

    """
    control = app.window().child_window(title="分区组", control_type="TreeItem", found_index=0)
    control.click_input(button='right', double=False)
    # 通过窗口标题获取顶层窗口
    top_window = app.top_window()
    if project_model == "V100":
        # cpu_name = "V100CPU"
        # 点击右键弹出的菜单项
        top_window.child_window(title="新建CPU", control_type="MenuItem").click_input()
        program.operation_window(win_name="创建新CPU", params={'input': [('名称：', cpu_name)], 'click': ["确定"]})
        top_window.child_window(title="新建分区", control_type="MenuItem").click_input()
        program.operation_window(win_name="创建新分区", params={'input': [('名称：', zone_name)], 'click': ["确定"]})
    else:
        # 点击右键弹出的菜单项
        top_window.child_window(title="新建分区", control_type="MenuItem").click_input()
        program.operation_window(win_name="创建新分区", params={'input': [('名称：', zone_name)], 'click': ["确定"]})


def tree_full(project_model, project_name, product_name, plug_name, zone_name):
    """
    创建完整构型树
        模板参数：(brief,"2023041902测试项目", "测试成品", "Plug", "测试分区")
        model：项目模板
        project_name：项目名称
        product_name：成品名称
        plug_name：插头名称
        zone_name：分区表数据包名称
    """
    create_project(project_model, project_name)
    create_product(project_name, product_name)
    create_plugs(product_name)
    create_plug(plug_name)

    # 增加所有总线
    add_all_bus()

    # 单独总线节点
    create_bus_point(plug_name, "1394", "1394CC总线", "1394CC")
    create_bus_point(plug_name, "1394", "1394CCDL总线", "1394CCDL")
    create_bus_point(plug_name, "1553", "1553总线", "1553")
    create_bus_point(plug_name, "422", "422总线", "422")
    if project_model == "V100":
        create_bus_point(plug_name, "TTE", "TTE总线", "TTE")

    # 创建未包含信号的数据包
    create_icd_empty(0, "1394CCDL", "1394CCDL测试ICD", "1H")
    create_icd_empty(1, "1394CC", "1394CC测试ICD", "2H")
    create_icd_empty(2, "1553", "1553测试ICD", "3H")
    create_icd_empty(3, "422", "422测试ICD", "4H")

    # 创建包含信号的数据包（最后的参数书是字节对齐方式）
    create_icd_and_single(0, "1394CCDL", "1394CCDL测试ICD", "20H", "1")
    create_icd_and_single(1, "1394CC", "1394CC测试ICD", "10H", "4")
    create_icd_and_single(2, "1553", "1553测试ICD", "30H", "2")
    create_icd_and_single(3, "422", "422测试ICD", "40H", "1")

    # 创建分区组
    create_zones(product_name)
    create_zone(project_model, zone_name)
    # 创建分区表
    create_icd_empty(4, "分区表", "测试分区表ICD", "1H")


if __name__ == '__main__':
    tree_full("V100", "20231109测试项目2", "测试成品2", "Plug", "测试分区")
    # create_product("20231109测试项目1", "测试成品1")
    # create_plugs("测试成品1")
    # create_plug("X1")
    # add_bus("1394CC", "1394CC", 1)
    # create_bus_point("X1", "1394", "1394CC", "1394总线节点")
    # create_icd_empty(0, "1394CC", "1394ICD", "1H")
    # create_project("brief", "2023041902测试项目")
    # create_product("2023041902测试项目", "测试成品")
    # create_plugs("测试成品")
    # create_zones("测试成品")
    # create_zone("测试分区")
    # 创建分区表
    # create_icd_empty(4, "分区表", "测试分区表ICD", "1H")
    # create_plug("Plug")
    # 增加单独总线
    # add_bus("1394CC总线", "1394CC", 1)
    # add_bus("1394CCDL总线", "1394CCDL", 2)
    # add_bus("1553总线", "1553", 3)
    # add_bus("422总线", "422", 4)
    # add_bus("TTE总线", "TTE", 5)
    # 单独总线节点
    # create_bus_point("Plug", "1394", "1394CC总线", "1394CC")
    # create_bus_point("Plug", "1394", "1394CCDL总线", "1394CCDL")
    # create_bus_point("Plug", "1553", "1553总线", "1553")
    # create_bus_point("Plug", "422", "422总线", "422")
    # create_bus_point("Plug", "TTE", "TTE总线", "TTE")
    # 增加所有总线
    # add_all_bus()
    # 创建未包含信号的数据包
    # create_icd_empty(1, "1394CCDL", "1394CCDL测试ICD", "1H")
    # create_icd_empty(0, "1394CC", "1394CC测试ICD", "2H")
    # create_icd_empty(2, "1553", "1553测试ICD", "3H")
    # create_icd_empty(3, "422", "422测试ICD", "4H")
    #     创建包含信号的数据包
    #     create_icd_and_single(0, "1394CCDL", "1394CCDL测试ICD1", "10H", "4")
    #     create_icd_and_single(1, "1394CC", "1394CC测试ICD1", "20H", "4")
        # create_icd_and_single(2, "1553", "1553测试ICD1", "30H", "4")
        # create_icd_and_single(3, "422", "422测试ICD1", "40H", "4")
